package com.panghu.controller;

import com.panghu.entity.*;
import com.panghu.service.impl.LoginServiceImpl;
import com.panghu.utils.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {
    @Autowired
    private LoginServiceImpl loginService;

    @PostMapping("/login")
    public ApiResult login(@RequestBody Login login) {
        //获取到用户名，密码
        Integer username = login.getUsername();
        String pwd = login.getPassword();

        //先到admin表中进行查询
        Admin admin = loginService.adminLogin(username, pwd);

        if (admin != null) {
            return ApiResultHandler.buildApiResult(200, "请求成功", admin);
        }

        //到teacher表中进行查询
        Teacher teacherRes = loginService.teacherLogin(username,pwd);
        if (teacherRes != null) {
            return ApiResultHandler.buildApiResult(200, "请求成功", teacherRes);
        }

        //到student表查询
        Student studentRes = loginService.studentLogin(username,pwd);
        if (studentRes != null) {
            return ApiResultHandler.buildApiResult(200, "请求成功", studentRes);
        }

        return ApiResultHandler.buildApiResult(400, "请求失败", null);

    }

}
